﻿using System;
using System.Data;
using System.Diagnostics;
using System.Net.Mail;
using System.Windows.Forms;

namespace wowSteadNotification
{
    static class Program
    {
        /// <summary>
        /// Main entrance for wowSteadNotification.
        /// </summary>
        [STAThread]
        static void Main(string[] cmdArgs)
        {
            wsn_BL wsnForum;

            if (cmdArgs.Length == 0)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new wsn_FE());
            }
            else
            {
                if (cmdArgs != null && cmdArgs[0] == "silent")
                {
                    // Common init
                    string evtMessage = "WowStead-Notfier\n***********************\n";
                    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
                    wowSteadNotification.Properties.Settings settings = new wowSteadNotification.Properties.Settings();
                    settings = Properties.Settings.Default;
                    wsnForum = new wsn_BL(settings.WoWSteadURL,
                        settings.KeksCharID,
                        settings.KeksPassHash,
                        settings.KeksUserID,
                        settings.KeksLastVisit);
                    // Start process
                    if (wsnForum.ForumPosts != null)
                    {
                        evtMessage += " 1. Latest posts aquired... (" + wsnForum.ForumPosts.Rows.Count.ToString() + " new posts)\n";
                        // Create RSS feed
                        if (settings.RSSFlag && wsnForum.createRSS(settings.RSSPath, settings.EMaiFrom))
                            evtMessage += " 2. RSS feed created...\n";
                        else
                            evtMessage += " 2. RSS feed not created...\n";

                        // Send mail on new items
                        if (wsnForum.ForumPosts.Rows.Count > 0)
                        {
                            if(wsnForum.mailNotifications(settings.MailSubject, 
                                settings.MailBody, settings.MailTrailer, 
                                settings.EMaiFrom, settings.EMailsNotification, 
                                settings.EMailServerSMTP))
                                evtMessage += " 3. Mail notification sent...\n";
                            else
                                evtMessage += " 3. Mail notification not sent...\n";
                        }
                        else
                        {
                            evtMessage += " 3. Mail notification not necessary...\n";
                        }
                    }
                    else
                    {
                        evtMessage += " 1. Errors occured (refer to error entry)\n";
                    }                    

                    // Write final event messages (information + errors)
                    EventLog.WriteEntry("WowStead-Notifier", evtMessage, EventLogEntryType.Information);
                    if (wsnForum.Errors != null && wsnForum.Errors.Count > 0)
                    {
                        string errormessages = "";
                        foreach (Exception error in wsnForum.Errors)
                            errormessages += error.Message + "\n******************\n";
                        EventLog.WriteEntry("WowStead-Notifier", errormessages, EventLogEntryType.Error);
                    }
                }            
            }
        }

    }
}
